<#assign assign_TableName = content.content.tableName>
<#assign camelName = content.content.camelName>
<#assign assign_PascalName = content.content.pascalName>
<#assign assign_EntityName =content.items.entity.className>
<#assign assign_EntityFullName = content.items.entity.packageName+"."+content.items.entity.className>
<#assign tableName = content.content.tableName>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${content.items.dao.packageName}.${content.items.dao.className}">
	<#--判断日期类型和枚举类型的时候不要判 !=''，只判null即可，否则会报错，布尔类型的时候不要不要判 !=''，只判null即可，否则值为false时保存不进去，尽量使用Boolean-->
	<!-- 通过对象中不为空的属性作为条件获取数据 -->
	<select id="query${camelName?cap_first}List" resultType="${assign_EntityFullName}">
		SELECT
			<#list content.content.fields as item>t.${item.name}<#if item?has_next>,</#if></#list>
		FROM ${assign_TableName} t
		<where>
		<#list content.content.fields as item>
			<#if item.primary != true>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>"> and t.${item.name} = ${r'#{'}${item.fieldName}${r'}'}</if>
			</#if>
		</#list>
			<if test="permissionSql != null and permissionSql != ''">@{permissionSql}</if>
		<#list content.content.fields as item>
			<#if item.primary == true>
			<if test="${item.fieldName} != null and ${item.fieldName} != ''"> and t.${item.name} = ${r'#{'}${item.fieldName}${r'}'}</if>
			</#if>
		</#list>
		</where>
	</select>


<#if content.content.primaryField??>
	<#assign assign_idFieldType = content.content.primaryField[0].fieldType>
	<#assign assign_idFieldName = content.content.primaryField[0].fieldName>
	<#assign assign_idName = content.content.primaryField[0].name>
	<!-- 通过ID获取数据 -->
	<select id="query${camelName?cap_first}" parameterType="${assign_idFieldType}" resultType="${assign_EntityFullName}">
		SELECT
			<#list content.content.fields as item>t.${item.name}<#if item?has_next>,</#if></#list>
		FROM ${assign_TableName} t
		WHERE t.${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
	</select>


	<!-- 插入一个对象包括属性值为null的值 -->
	<insert id="add${camelName?cap_first}" parameterType="${assign_EntityFullName}">
		INSERT INTO ${assign_TableName}
		<trim prefix="(" suffix=")" suffixOverrides=",">
		<#list content.content.fields as item>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>">${item.name}<#if item?has_next>,</#if></if>
		</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
		<#list content.content.fields as item>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>">${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></if>
		</#list>
		</trim>
	</insert>


	<!-- 更新一个对象中属性不为null值,条件为对象中的id -->
	<update id="modify${camelName?cap_first}" parameterType="${assign_EntityFullName}">
		UPDATE ${assign_TableName}
		<set>
		<#list content.content.fields as item>
			<#if item.primary != true>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>">${item.name} = ${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></if>
			</#if>
		</#list>
		</set>
		WHERE ${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
	</update>


	<!-- 通过ID删除数据 -->
	<delete id="delete${camelName?cap_first}" parameterType="${assign_idFieldType}">
		DELETE FROM ${assign_TableName} WHERE ${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
	</delete>

</#if>
</mapper>